import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { start, close } from '@/utils/nprogress'
const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/',
    redirect: '/index',
    component: () => import('../components/layout/Layout.vue'),
    children: [
      {
        path: "index",
        name: 'index',
        component: () => import('../views/index/index.vue'),
      },
      {
        path: "topology",
        name: 'topology',
        component: () => import('../views/topology/index.vue'),
      },
      {
        path: "facility",
        name: 'facility',
        component: () => import('../views/facility/index.vue'),
      },
      {
        path: "watch",
        name: 'watch',
        component: () => import('../views/watch/index.vue'),
      },
      {
        path: "warning",
        component: () => import('../views/warning/index.vue'),
        children: [
          {
            path: "",
            component: () => import('../views/warning/warning.vue'),
          },
          {
            path: "warningDetail",
            name: 'warningDetail',
            component: () => import('../views/warning/warningDetail.vue'),
          }
        ]
      },
      {
        path: "setting",
        name: 'setting',
        component: () => import('../views/setting/index.vue'),
      },
      {
        path: "ai-diagnosis",
        name: 'ai-diagnosis',
        component: () => import('../views/ai-diagnosis/index.vue'),
      },
      {
        path: "ai-faq",
        name: 'ai-faq',
        component: () => import('../views/ai-faq/index.vue'),
      },
      {
        path: "users",
        name: 'users',
        component: () => import('../views/users/index.vue'),
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
//路由拦截
router.beforeEach((to, form, next) => {
  start()
  if (to.fullPath === '/login' || to.params.pathMatch) {
    next()
  } else {
    if (!localStorage.getItem("token")) {
      next({ path: "/login" })
    }
    next()
  }

})
router.afterEach((to, form, next) => {
  close()
})
export default router
